package com.bytedance.scenedemo.group.async_inflate;

import com.bytedance.scene.group.AsyncLayoutGroupScene;
import com.bytedance.scene.group.GroupScene;
import com.bytedance.scenedemo.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ProgressBar;
import ohos.agp.components.StackLayout;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by JiangQi on 9/19/18.
 */
public class AsyncInflateSceneDemo extends GroupScene {
    private int id = 0;
    private final static int num100 = 100;
    private final static int num10 = 10;
    public static final int CODE_DOWNLOAD_FILE1 = 1;

    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {

        ComponentContainer componentContainer = (ComponentContainer) inflater.parse(ResourceTable.Layout_asyncinflate_scene_demo,container,false);
        id = ResourceTable.Id_async_stack;
        return componentContainer;
    }

    @Override
    public void onViewCreated(Component component, Intent intent) {
        super.onViewCreated(component, intent);

        TestAsyncInflateScene scene = new TestAsyncInflateScene();
        scene.setAsyncLayoutEnabled(true);
        add(id, scene, "wo");
    }

    private static class TestAsyncInflateScene extends AsyncLayoutGroupScene{
        EventRunner runner = EventRunner.getMainEventRunner();
        ProgressBar progressBar = null;
        Image imageClock;
        private int progress = 0;
        private  EventHandler myHandler = new EventHandler(runner){
            @Override
            protected void processEvent(InnerEvent event) {
                super.processEvent(event);
                if (event == null) {
                    return;
                }
                int eventId = event.eventId;
                if(eventId == CODE_DOWNLOAD_FILE1){
                    if(progress>num100){
                     progress = 0;
                    }
                    progress+=num10;
                    if(progressBar!=null){
                        progressBar.setProgressValue(progress);
                    }
                }
            }
        };

        @Override
        protected int getLayoutId() {
            return ResourceTable.Layout_test_async_layout;
        }

        @Override
        public void onAsyncViewCreated(Component view, Intent savedInstanceState) {
            super.onAsyncViewCreated(view, savedInstanceState);
            progressBar= findViewById(ResourceTable.Id_pro);
            imageClock = findViewById(ResourceTable.Id_imag_clock);
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    myHandler.sendEvent(CODE_DOWNLOAD_FILE1);
                }
            },0,num100);

            imageClock.addDrawTask((component,canvas) ->{drawCircle(component,canvas);});

            animationClock();
        }

        private void drawCircle(Component component, Canvas canvas) {
            int height = component.getHeight();
            Paint paint = new Paint();
            paint.setColor(new Color(Color.getIntColor("#BF8F9F")));
            paint.setStyle(Paint.Style.STROKE_STYLE);
            paint.setStrokeWidth(40);
            canvas.drawCircle((float) height / 2,(float) height / 2,110,paint);
        }

        private void animationClock() {
            AnimatorProperty animatorProperty = imageClock.createAnimatorProperty();
            animatorProperty.alpha(95).alphaFrom(0).setDuration(1000).setLoopedCount(Animator.INFINITE);
            imageClock.setBindStateChangedListener(new Component.BindStateChangedListener() {
                @Override
                public void onComponentBoundToWindow(Component component) {
                    animatorProperty.start();
                }

                @Override
                public void onComponentUnboundFromWindow(Component component) {
                    animatorProperty.stop();
                }
            });
        }
    }
}
